<?php
require_once("database.php");
$_SESSION[$sessionDepth] == "";
$bodyString = "";
if ($_SERVER['REQUEST_METHOD']=="POST") {
	$id = $_POST['id'];

	if ($id) {
		$sort_order = htmlentities($_POST["sort_order"],ENT_QUOTES);
		$timestamp = htmlentities($_POST["timestamp"],ENT_QUOTES);
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$publishDate = htmlentities($_POST["publishDate"],ENT_QUOTES);
		$body = htmlentities($_POST["body"],ENT_QUOTES);

		$SQLeditQuery = "UPDATE best_news SET ";	
		$SQLeditQuery .= "  sort_order = '$sort_order',  timestamp = FROM_UNIXTIME($timestamp),  title = '$title',  publishDate = '$publishDate',  body = '$body'  "; 
		$SQLeditQuery .= " WHERE id = '$id' ";
		$SQLeditResult = $db->query($SQLeditQuery);
	}
	else {
		$SQLsortQuery = "SELECT MAX(sort_order) AS sort_order FROM best_news";
		$SQLsortResult = $db->query($SQLsortQuery);
		$sortArray = $db->fetch_array($SQLsortResult);
		$sort_order = htmlentities($sortArray['sort_order']);
		$sort_order++;

		
		$timestamp = htmlentities($_POST["timestamp"],ENT_QUOTES);
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$publishDate = htmlentities($_POST["publishDate"],ENT_QUOTES);
		$body = htmlentities($_POST["body"],ENT_QUOTES);

$SQLnewQuery = "INSERT INTO best_news (sort_order,timestamp,title,publishDate,body) VALUES ('$sort_order',CURDATE(), '$title','$publishDate','$body')";
		$SQLnewResult = $db->query($SQLnewQuery);
	} //end if(id) else
} // end if (request method = POST)

$SQLlistQuery = "SELECT * FROM best_news ORDER BY sort_order ASC";
$SQLlistResult = $db->query($SQLlistQuery);
while ($rowArray = $db->fetch_array($SQLlistResult)) {
		$sort_order = $rowArray["sort_order"];
		$timestamp = $rowArray["timestamp"];
		$title = $rowArray["title"];
		$publishDate = $rowArray["publishDate"];
		$body = $rowArray["body"];
		$id = $rowArray["id"];

		$bodyString .= "\n<div class='backListRecord best_news_record' id='$id'>";
		$bodyString .= "\n  <div class='backListElement'><div class='dragIcon'></div></div>";
		$bodyString .= "\n  <div class='backListElement'>Date and Time Published : ".strftime('%m',strtotime($timestamp))."-".strftime('%d',strtotime($timestamp))."-".strftime('%Y',strtotime($timestamp))."</div>";
		$bodyString .= "\n  <div class='backListElement backListTitle'>$title</div>";
		$bodyString .= "\n  <div class='backListElement'>Date / Month Published: $publishDate</div>";
						
		$bodyString .= "
  <div class='listPageButtons'>
    <a class='editBtn positiveBtns' href='best_news_write.php?id=$id'>EDIT</a>
      <div class='deleteBtn'>
      <a class='negativeBtns'>DELETE</a>
      <div class='deleteConfirmation' id='deleteConfirmation$id'>
        Do you really want to delete? <a class='deleteYes'>YES</a> // <a class='deleteNo'>NO</a>
      </div>
    </div>
  </div>
</div>  <!--end .backListRecord id:$id-->
";
}
?>
<html>
	<head>
		<title>News List</title>
		<?php require_once('io_listPage_includes.php'); ?>
	<script type='text/javascript'>
	var jsTableName = 'best_news';

	$(function() {
		jQuery.fileDelete = function(id,field) {
			$.ajax({
				url: "ajax.php",
				data: "tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile",
				cache: false,
				complete: function(){
					//alert("tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile");
				}
			});
		};	

		jQuery.deleteAllFiles = function(id,field) {
					
		};		
	});
	</script>
	</head>
<body>
	<div class='navigation'>
	  <div id='goTo'>Menu...</div>
	  <div id="tableList">
    <?php 
      $currentNavTable = 'best_news';
      require_once('navlist.php');
    ?>
	  </div> <!-- end #tableList -->
	  <div class='siteName'>TheBest.com</div>
	    <div id='currentTableTitle'><a href='best_news_list.php'>News</a></div>
	    <div id='listBtns'>
	      <div id='listViewBtn'><img src='images/list-icon.gif' alt='List View' /></div>
	      <div id='gridViewBtn'><img src='images/grid-icon.gif' alt='Grid View' /></div>
	    </div>
	  </div><!-- End .navigation -->
	<div class='recordsList sortable'>
  <!-- <a class='newBtn positiveBtns' href='best_news_write.php'>NEW RECORD</a> -->
	  <?php echo $bodyString; ?>
	  <a class='newBtn positiveBtns' href='best_news_write.php'>NEW RECORD</a>
	</div>
	

	</body>
</html>